package com.suda.sudaexamsystem.interceptor;

import com.suda.sudaexamsystem.pojo.User;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断用户是否登录
        User user = (User) request.getAttribute("user");
        if (user == null) {
            log.info("用户未登录");
//            response.sendRedirect("/users/login");
            request.getRequestDispatcher("/users/login").forward(request, response);
            return false;
        }
        return true;
    }
}
